﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using SLS.DataBase;
using SLS.ExClassLib.Events;
using LanSunStudio.ExClassLib;
using SLS.ExClassLib.Message;
using SLS.ExClassLib.Loc;
using LanSunStudio.ExClassLib.Tools;
using SLS.ExClassLib.WCFClient;
using SLS.Core.Database;
namespace SLS.ExClassLib.Database
{
    /// <summary>
    /// Class that holds everything that need to be binded to the main window
    /// </summary>
    public partial class DataAccessService
    {
        #region =================SINGLETON=================
        /// <summary>
        /// Singleton
        /// </summary>
        public static readonly DataAccessService Instance = new DataAccessService();
        public ResourceManager ResourceManager = new ResourceManager();
        private ObservableCollection<DataSource> _DataSources = new ObservableCollection<DataSource>();
        public ObservableCollection<DataSource> DataSources
        {
            get
            {
                return _DataSources;
            }
            set
            {
                if (_DataSources != value)
                    _DataSources = value;
            }
        }
        public string AppXMLPath
        {
            get
            {
                return Appliationpath + @"\ExXML\";
            }
        }
        public ArrayList DataList
        {
            get;
            set;
        }
        public string Appliationpath;
        public log4net.ILog m_log;
        /// <summary>
        /// Private constructor for singleton pattern
        /// </summary>
        private DataAccessService()
        {
            BaseMessage = new MessageEx();
            BaseMessage.AgentID = "LANSUN";//这里需要有合适的定义启动的AgentID的方式。
            dwUser = GlobalDataSet.Tables["UserManagement"].NewRow();
            dwUser["SM"] = "1";
            dwUser["AGENTID"] = "LANSUN";
            Appliationpath = PathHelper.ApplicationPath;
            DataList = new ArrayList();
        }
        public void Load()
        {
            try
            { 
                BLClient = new ERPBLayerServiceClient();
                BLClient.Open(); 
                DataSet setLogin = GetLogInDataSet();
                GlobalDataSet.Merge(setLogin);
                UpdateGlobalDataSet();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        #endregion

        #region =================PROPERTIES=================  
        public MessageEventType TracerLevel = MessageEventType.VERBOSE;
        SLDBSET m_LD;
        public SLDBSET GlobalDataSet
        {
            get
            {
                if (m_LD == null)
                {
                    m_LD = new SLDBSET();
                }
                return m_LD;
            } 
        }
        public SLQDBSET setQueryCond = new SLQDBSET();

        private ERPBLayerServiceClient m_BLClient = null;
        public ERPBLayerServiceClient BLClient
        {
            get { return m_BLClient; }
            set { m_BLClient = value; }
        }
        public MessageEx BaseMessage = null; 
        public DataRow dwUser;
        private string m_UIXMLPath;
        public string UIXMLPath
        {
            get { return m_UIXMLPath; }
            set { m_UIXMLPath = value; }
        }
        public DataTable TBTableList
        {
            get
            {
                return GlobalDataSet.TABLELIST;
            }
        }
        public DataTable TBForeignKeyList
        {
            get
            {
                return GlobalDataSet.FOREIGNKEYLIST;
            }
        }

        public SLDBSET setGlobal
        {
            get
            {
                return GlobalDataSet;
            }
        }
        DataRow[] dwForeignKeyLists;
        string localTableList = "MenuBuild;AgentList;FOREIGNKEYLIST;GOODSTYPE;GTDEF;GTKR;GTAR;GTPR;";
        private List<MessageInfo> _LogEvents = new List<MessageInfo>(200);
        /// <summary>
        /// Log info list binded to the LogExplorer
        /// </summary>
        public List<MessageInfo> LogEvents
        {
            get { return _LogEvents; }
            set
            {
                if (_LogEvents != value)
                    _LogEvents = value;
                else return; ;
            }
        }

        #endregion 

        #region =================WriteInfoLog=================
        public void WriteInfoLog(string msg)
        {
            if (m_log == null)
                return;
            if (msg.Contains("ERROR:"))
            {
                m_log.Error(msg);
                return;
            }
            m_log.Info(msg);
        }
        public string Extend2String(DataSet setMsg)
        {
            string strObj = "";
            try
            {
                if (setMsg == null)
                    return strObj;
                foreach (DataTable tableTemp in setMsg.Tables)
                {
                    foreach (DataRow rowTemp in tableTemp.Rows)
                    {
                        strObj += Extend2String(rowTemp) + "; ";
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error("DataAccessService.Extend2String", ex);
            }
            return strObj;
        }
        public string Extend2String(Queue rowQueue)
        {
            string strObj = "";
            Array arrayQ = rowQueue.ToArray();
            try
            {
                strObj = arrayQ.ToString();
            }
            catch (Exception ex)
            {
                Tracer.Error("DataAccessService.Extend2String", ex);
            }
            return strObj;
        }
        public string Extend2String(DataRow rowObj)
        {
            string strObj = "";
            string FieldName = "";
            try
            {
                strObj += "DataTabale " + rowObj.Table.TableName + ": " + Environment.NewLine;
                FieldName = "GTKEY";
                if (rowObj.Table.Columns.Contains(FieldName))
                    strObj += rowObj[FieldName] + ";";
                FieldName = "GDKEY";
                if (rowObj.Table.Columns.Contains(FieldName))
                    strObj += rowObj[FieldName] + ";";
                return strObj;
                foreach (DataColumn dc in rowObj.Table.Columns)
                {
                    FieldName = dc.ColumnName;
                    strObj += FieldName + ":" + rowObj[FieldName] + ";";
                }
            }
            catch (Exception ex)
            {
                Tracer.Error("DataAccessService.Extend2String", ex);
            }
            return strObj;
        }
        #endregion

    }
}
